#include "common.h"
#include "led.h"
#include "delay.h"
#include "clk.h"
#include "uart.h"
#include "key.h"
#include "printf.h"
#include "gic.h"
#include "i2c.h"
#include "ft3x68_core.h"


void init_pins(void)
{
	/* led pins */
    IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER3_GPIO5_IO03, 0U);
    IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER3_GPIO5_IO03, 
                        IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
                        IOMUXC_SW_PAD_CTL_PAD_DSE(1U) |
                        IOMUXC_SW_PAD_CTL_PAD_HYS_MASK);

	/* uart1 pins */	
	IOMUXC_SetPinMux(IOMUXC_UART1_RX_DATA_UART1_RX, 0U);
	IOMUXC_SetPinConfig(IOMUXC_UART1_RX_DATA_UART1_RX, 
						IOMUXC_SW_PAD_CTL_PAD_DSE(2U) |
						IOMUXC_SW_PAD_CTL_PAD_SPEED(2U) |
						IOMUXC_SW_PAD_CTL_PAD_PKE_MASK);
	IOMUXC_SetPinMux(IOMUXC_UART1_TX_DATA_UART1_TX, 0U);
	IOMUXC_SetPinConfig(IOMUXC_UART1_TX_DATA_UART1_TX, 
						IOMUXC_SW_PAD_CTL_PAD_DSE(2U) |
						IOMUXC_SW_PAD_CTL_PAD_SPEED(2U) |
						IOMUXC_SW_PAD_CTL_PAD_PKE_MASK);

	/* KEY1 pins GPIO5_1 SNVS_TAMPER1 */
	IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01, 0U);
	IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01, 
						IOMUXC_SW_PAD_CTL_PAD_DSE(6U) |
						IOMUXC_SW_PAD_CTL_PAD_SPEED(2U) |
						IOMUXC_SW_PAD_CTL_PAD_PKE_MASK |
						IOMUXC_SW_PAD_CTL_PAD_HYS_MASK);

	/* KEY2 pins GPIO4_IO14 */
	IOMUXC_SetPinMux(IOMUXC_NAND_CE1_B_GPIO4_IO14, 0U);
	IOMUXC_SetPinConfig(IOMUXC_NAND_CE1_B_GPIO4_IO14, 
						IOMUXC_SW_PAD_CTL_PAD_DSE(6U) |
						IOMUXC_SW_PAD_CTL_PAD_SPEED(2U) |
						IOMUXC_SW_PAD_CTL_PAD_PKE_MASK |
						IOMUXC_SW_PAD_CTL_PAD_HYS_MASK);

    //init i2c2
    IOMUXC_SetPinMux(IOMUXC_UART5_TX_DATA_I2C2_SCL, 1);
	IOMUXC_SetPinMux(IOMUXC_UART5_RX_DATA_I2C2_SDA, 1);
	IOMUXC_SetPinConfig(IOMUXC_UART5_TX_DATA_I2C2_SCL, 0x70B0);
	IOMUXC_SetPinConfig(IOMUXC_UART5_RX_DATA_I2C2_SDA, 0X70B0);


    /*GPIO1_IO01 as  ft5x006 int*/
  	IOMUXC_SetPinMux(IOMUXC_GPIO1_IO01_GPIO1_IO01, 0U);
	IOMUXC_SetPinConfig(IOMUXC_GPIO1_IO01_GPIO1_IO01, 
						IOMUXC_SW_PAD_CTL_PAD_DSE(6U) |
						IOMUXC_SW_PAD_CTL_PAD_SPEED(2U) |
						IOMUXC_SW_PAD_CTL_PAD_PKE_MASK |
						IOMUXC_SW_PAD_CTL_PAD_HYS_MASK);
	//init SNVS_TAMPER4 to ft5xxxx of rst
        IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER4_GPIO5_IO04,0);
	IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER4_GPIO5_IO04,0X10B0);
	GPIO5->GDIR |= (1<<4);
	GPIO5->DR |=(1<<4);
}
#if 0	
void length_test(void)
{
 printf("sizeof(char)=%d\r\n",sizeof(char));
 printf("sizeof(signed short)=%d\r\n",sizeof(signed short));
 printf("sizeof(signed int)=%d\r\n",sizeof(signed int));
 printf("sizeof(signed long long)=%d\r\n",sizeof(signed long long));
 printf("sizeof(uint8_t)=%d\r\n",sizeof(uint8_t));
 printf("sizeof(int16_t)=%d\r\n",sizeof(int16_t));
 printf("sizeof(uint32_t)=%d\r\n",sizeof(uint32_t));
 printf("sizeof(uint64_t)=%d\r\n",sizeof(uint64_t));
}
#endif
void printException(unsigned int cpsr, char *str)
{
	printf("Exception! cpsr is 0x%x\r\n", cpsr);
	printf("%s\r\n", str);
}

void printSWIVal(unsigned int *pSWI)
{
	printf("SWI val = 0x%x\r\n", *pSWI & ~0xff000000);
}
	

int  main()
{
	u8 vendorid;
	u8 c;
	init_pins();
	led_gpio_init();
	delay_init();
	led_ctl(0);//turn off led
	boot_clk_gate_init();
	boot_clk_init();
	uart1_init();
	puts("hello world\r\n");
	system_init_irq_table();
	key_irq_init();时
	ft3x68_irq_init();
	gic_init();
	gic_enable_irq(GPIO5_Combined_0_15_IRQn);
	gic_enable_irq(GPIO4_Combined_0_15_IRQn);
	gic_enable_irq(GPIO1_Combined_0_15_IRQn);
	i2c_init(I2C2);
	ft3x68_hardware_reset();
	if(ft3x68_read_chipid())
	{
	  vendorid=ft3x68_get_vendorid();
	  ft3x68_get_upradefile(vendorid);
      ft3x68_firmware_upgrade();
	}
	ft3x68_hardware_reset();
	ft3168_rawdata_test();
	ft3x68_hardware_reset();
	#if 0
	length_test();
	fts_test_log();
	#endif
	while(1)
	{	
	    #ifdef FT3X68_ESD_CHECK
	    delayms(5);
		ft3x68_esdcheck_algorithm();
		delayms(5);
		#endif
		#if 1
		c = getc();
		if (c == '\r')
		{
			putc('\n');
		}
		if (c == '\n')
		{
			putc('\r');
		}
		putc(c);
		#endif

	}
					
	return 0;
}


